package com.ruoyi.web.controller.substation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SParameterDetails;
import com.ruoyi.system.service.ISParameterDetailsService;
import com.ruoyi.system.service.ISProtectDeviceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SProtectDeviceVersion;
import com.ruoyi.system.service.ISProtectDeviceVersionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备的保护装置的参数列版本信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Api(tags = "设备的保护装置的参数列版本信息管理")
@RestController
@RequestMapping("/substation/version")
public class SProtectDeviceVersionController extends BaseController
{
    @Autowired
    private ISProtectDeviceVersionService sProtectDeviceVersionService;

    @Autowired
    private ISParameterDetailsService parameterDetailsService;

    /**
     * 查询设备的保护装置的参数列版本信息列表
     */
    @ApiOperation("获取设备的保护装置的参数列版本信息列表")
    @PreAuthorize("@ss.hasPermi('substation:version:list')")
    @GetMapping("/list")
    public TableDataInfo list(SProtectDeviceVersion sProtectDeviceVersion)
    {
        startPage();
        List<SProtectDeviceVersion> list = sProtectDeviceVersionService.list(sProtectDeviceVersion.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 获取变电站设备保护装置的参数列表的最新三个版本
     */
    @ApiOperation("获取变电站设备保护装置的参数列表的最新三个版本")
    @PreAuthorize("@ss.hasPermi('substation:version:list')")
    @GetMapping("/versionList")
    public AjaxResult versionList(SProtectDeviceVersion sProtectDeviceVersion)
    {
        QueryWrapper<SProtectDeviceVersion> queryWrapper = sProtectDeviceVersion.queryWrapper();
        queryWrapper.orderByDesc("file_date");
        queryWrapper.last("limit 0, 3");
        List<SProtectDeviceVersion> list = sProtectDeviceVersionService.list(queryWrapper);

        List<Map<String, String>> mapList = new ArrayList<>();
        for (int i = 0; i < Math.min(list.size(), 3); i++) {
            Map<String, String> map = new HashMap<>();
            map.put("value", list.get(i).getId());
            map.put("label", list.get(i).getFileDate());
            mapList.add(map);
        }
        return success(mapList);
    }

    /**
     * 导出设备的保护装置的参数列版本信息列表
     */
    @ApiOperation("导出设备的保护装置的参数列版本信息列表")
    @PreAuthorize("@ss.hasPermi('substation:version:export')")
    @Log(title = "设备的保护装置的参数列版本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SProtectDeviceVersion sProtectDeviceVersion)
    {
        List<SProtectDeviceVersion> list = sProtectDeviceVersionService.list(sProtectDeviceVersion.queryWrapper());
        ExcelUtil<SProtectDeviceVersion> util = new ExcelUtil<SProtectDeviceVersion>(SProtectDeviceVersion.class);
        util.exportExcel(response, list, "设备的保护装置的参数列版本信息数据");
    }

    /**
     * 获取设备的保护装置的参数列版本信息详细信息
     */
    @ApiOperation("获取设备的保护装置的参数列版本信息详细")
    @ApiImplicitParam(name = "id", value = "设备的保护装置的参数列版本信息ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:version:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        SProtectDeviceVersion protectDeviceVersion = sProtectDeviceVersionService.getById(id);
        if(StringUtils.isNotNull(protectDeviceVersion)) {
            List<SParameterDetails> list = parameterDetailsService.list(new LambdaQueryWrapper<SParameterDetails>()
                    .eq(SParameterDetails::getDeviceProtectVersionId, id));
            protectDeviceVersion.setParameterDetailsList(list);
        }
        return success(protectDeviceVersion);
    }

    /**
     * 新增设备的保护装置的参数列版本信息
     */
    @ApiOperation("新增设备的保护装置的参数列版本信息")
    @PreAuthorize("@ss.hasPermi('substation:version:add')")
    @Log(title = "设备的保护装置的参数列版本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SProtectDeviceVersion sProtectDeviceVersion)
    {
        return toAjax(sProtectDeviceVersionService.save(sProtectDeviceVersion));
    }

    /**
     * 修改设备的保护装置的参数列版本信息
     */
    @ApiOperation("更新设备的保护装置的参数列版本信息")
    @PreAuthorize("@ss.hasPermi('substation:version:edit')")
    @Log(title = "设备的保护装置的参数列版本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SProtectDeviceVersion sProtectDeviceVersion)
    {
        return toAjax(sProtectDeviceVersionService.updateById(sProtectDeviceVersion));
    }

    /**
     * 删除设备的保护装置的参数列版本信息
     */
    @ApiOperation("删除设备的保护装置的参数列版本信息")
    @ApiImplicitParam(name = "ids", value = "设备的保护装置的参数列版本信息ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:version:remove')")
    @Log(title = "设备的保护装置的参数列版本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(sProtectDeviceVersionService.removeByIds(ids));
    }
}
